var setting = {
	async: {
		enable: true,
		url: getUrl
	},
	data: {
		simpleData: {
		  enable: true
	    }
	},
	view: {
		expandSpeed: ""
	},
	callback: {
		beforeExpand: beforeExpand,
		onAsyncSuccess: onAsyncSuccess,
		onAsyncError: onAsyncError
	}
};
function getUrl(treeId, treeNode) {
	var sign=treeNode.id.substr(0,1);
	var cid=treeNode.id.substr(1);
	if(sign=='C'){
		return "../base/queryTree_Ban.action?cid="+cid+"&urlSign=anmeterread&ymonth="+$("#ymonth").val()+"&currentTime="+$("#ctime").val();
	}else if(sign=="B"){
		return "../base/queryTree_Unit.action?bid="+cid+"&urlSign=anmeterread&ymonth="+$("#ymonth").val()+"&currentTime="+$("#ctime").val();
	}else if(sign=="U"){
		return "../base/queryTree_Room.action?uid="+cid+"&urlSign=anmeterread&ymonth="+$("#ymonth").val()+"&currentTime="+$("#ctime").val();
	}
};
function beforeExpand(treeId, treeNode) {
	if (!treeNode.isAjaxing) {
		ajaxGetNodes(treeNode, "refresh");
		return true;
	} else {
		alert("正在下载数据中，请稍后展开节点。。。");
		return false;
	}
};
function ajaxGetNodes(treeNode, reloadType) {
	var zTree = $.fn.zTree.getZTreeObj("menup");
	if (reloadType == "refresh") {
		treeNode.icon = "../script/ztree/css/zTreeStyle/img/loading.gif";
		zTree.updateNode(treeNode);
	}
	zTree.reAsyncChildNodes(treeNode, reloadType, true);
};
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
	var zTree = $.fn.zTree.getZTreeObj("menup");
	alert("异步获取数据出现异常。");
	treeNode.icon = "";
	zTree.updateNode(treeNode);
};
function onAsyncSuccess(event, treeId, treeNode, msg) {
	var zTree = $.fn.zTree.getZTreeObj("menup"),
	totalCount = treeNode.count;
	treeNode.icon = "";
	zTree.updateNode(treeNode);
	zTree.selectNode(treeNode.children[0]);
};

$(document).ready(function(){
	$.post("../base/query_RoomInfo.action",{"urlSign":"anmeterread","ymonth":$("#ymonth").val()},function(result){
		$.fn.zTree.init($("#menup"), setting, result.communitys);
	});
	
});
function saveAnmeterRead(){
	$('#myModal').modal('show');
	var anmeterreadForm = $("#anmeterread").formToArray();
	$.post("../dashboard/saveRead_Watermeter.action",anmeterreadForm,function(result){
		if(result.msg=="succ"){
  			$('#myModal').modal('hide');
  			$("#alertinfo").html("水表读数维护成功");
  			$('#myinfo').modal('show');
  		}else{
  			$('#myModal').modal('hide');
  			$("#alertinfo").html(result.msg);
  			$('#myinfo').modal('show');
  		}
	});
};
function showAnmeterRead(unitid,urlSign){
	var ymonth=$("#ymonth").val();
	window.location.assign("../dashboard/queryReadForm_Watermeter.action?unitid="+unitid+"&urlSign="+urlSign+"&ymonth="+ymonth+"&currentTime="+$("#ctime").val());
}
function showDateRead(){
	var unitid=$("#unitid").val();
	if(unitid!=null && unitid!="" && unitid!=undefined){
		var ymonth=$("#ymonth").val();
		window.location.assign(basePath+"/dashboard/queryReadForm_Watermeter.action?unitid="+unitid+"&urlSign=anmeterread&ymonth="+ymonth+"&currentTime="+$("#ctime").val());
	}
}
function countmeter(currentobj){
	var trobj=$(currentobj).closest('#displayrow');
	var thereadings=$(trobj.find("#thereadings")[0]).val();
	var currentreading=$(trobj.find("#currentreading")[0]).val();
	
	/**计算走表量*/
	if(currentreading==null || currentreading=='' || currentreading==undefined){
		currentreading='0';
	}
	if(checknum(currentreading)){
		$(trobj.find("#walkquantity")[0]).val(changeTwoDecimal_f(parseFloat(currentreading,10)-parseFloat(thereadings,10)));
	}
	var walkquantity=$(trobj.find("#walkquantity")[0]).val();
	var amount=$(trobj.find("#amount")[0]).val();
	var charge=$(trobj.find("#charge")[0]).val();
	/*计算计费量*/
	if(amount==null || amount=='' || amount==undefined){
		amount='0';
	}
	if(walkquantity==null || walkquantity=='' || walkquantity==undefined){
		walkquantity='0';
	}
	if(checknum(amount)){
		$(trobj.find("#charge")[0]).val(changeTwoDecimal_f(parseFloat(amount,10)+parseFloat(walkquantity,10)));
	}
}
function checknum(param){
	var reg = new RegExp("^[0-9]+(.[0-9]{2})?$");  
	if(!reg.test(param)){  
		alert("请输入最多带有两位小数的数字。例如：19.98"); 
		return false;
	}  
	return true;
}